<?php
  require_once('includes/db.php');
  require_once('includes/header.php');
pr1($_GET);
  #################
  $tableName = 'products';
  #################
  $id = isset($_GET['id']) ? $_GET['id'] : 0;
  $flag = false;

  ######### START DELETE ##################
  if(isset($_GET['mode']) &&$_GET['mode']=='del') {
    $numRows = deleteById($tableName, $id);
    if($numRows == 1){
      echo success("Record deleted successfully.");
    }
  }
  ######### END DELETE  ###################

  if(isset($_GET['status'])){
    $newStatus = $_GET['status']==0 ? 1 :0;
    $data = array('status'=>$newStatus);
    $flag = updateById($tableName,$data, $id);
    pr1($newStatus);
  }

  ########### SEARCH START ###########################
  $search = isset($_GET['search'])? trim($_GET['search']) : '';
  $where = '';
  if($search != '') {
    $where = "`name` LIKE '%$search%'";
  }
  ########### SEARCH END ###########################

  ########### Count total records in users table ##############
  $total_users = countAll($tableName,$where);
  ########### Count total records in users table ##############


  ########### PAGING START ###########################

  $rpp = 7;// Record Per Page (offset)
  $total_pages = ceil($total_users / $rpp);
  $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
  $index = $rpp*($currentPage-1); // limit or from where we have to start fetching

  ############# SORTING START ##########################
  $orderArray = array('asc','desc');
  $fieldArray = array('status','added_date','name');


//  $order = isset($_GET['order']) ? $_GET['order'] : 'asc';
// if(!in_array($order, $orderArray)){
//    $order = 'asc';
//  }
 $field = ( isset($_GET['field']) && in_array($_GET['field'], $fieldArray) ) ?
           $_GET['field'] : 'added_date';

  $order = ( isset($_GET['order']) && in_array($_GET['order'], $orderArray) ) ?
           $_GET['order'] : 'asc';

  $newOrder = $order == 'asc' ? 'desc' : 'asc';
  $sortClass = $order == 'asc'?'icon-sort-by-alphabet' :'icon-sort-by-alphabet-alt';
  ############# SORTING END ##########################

?>

    <div class="container">
<?php  if($flag) {

?>
<div class="alert alert-success alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  <strong>Warning!</strong> Best check yo self, you're not looking too good.
</div>
<?php }?>
<!------ All Users Start --------->
<div class="table-responsive">
  <h3>Total Records <span class="label label-warning"><?=$total_users?></span> Sorted By <em><?=$field?> <?=$order?></em></h3>

  <div class="search">
     <form class="navbar-form navbar-left" role="search" method="get">
      <div class="form-group">
        <input type="text" name="search" class="form-control" placeholder="Search">
      </div>
      <button type="search" class="btn btn-info">Search&nbsp;<i class="icon-search"></i></button>
    </form>
  </div><!---end search--->

  <table class="table table-hover table-bordered">

    <thead>
      <tr>
        <th>#id</th>

        <th><a href="?search=<?=$search?>&page=<?=$currentPage?>&field=name&order=<?=$newOrder?>">Name <i class="<?=$sortClass?>"></i></a></th>

        <th>Image <i class="icon-sort-by-alphabet"></i></th>
        <th>Added Date</th>
        <th>
<?php if($order == 'desc'){ ?>
          <a href="?search=<?=$search?>&page=<?=$currentPage?>&field=status&order=<?=$newOrder?>">Status <i class="icon-long-arrow-down"></i></a>
<?php } else {?>
          <a href="?search=<?=$search?>&page=<?=$currentPage?>&field=status&order=<?=$newOrder?>">Status <i class="icon-long-arrow-up"></i></a>
<?php } //end else?>
        </th>
        <th>Options</th>
      </tr>
    </thead>

    <tbody>
<?php



  ###############################################
  $result = fetchAll($tableName,$where,"$field $order",$index,$rpp);
  ###############################################
  while($ans = mysql_fetch_assoc($result))
  {
//pr1($ans);
  $id = $ans['id'];
  $status = getStatus($ans['status']);
  $status = "<a style=\"text-decoration:none\" href=\"?id=$id&status=$ans[status]&search=$search&page=$currentPage&field=$field&order=$order\">$status </a>";
?>
      <tr>
        <td><?=$id?></td>
        <td><?=$ans['name']?></td>
        <td><img height="90" width="120" src="uploads/products/<?=$ans['image']?>"></a></td>
        <td><?=$ans['added_date']?></td>
        <td><?=$status?></td>
        <td>
          <!--Edit--->
          <a href="edit_product.php?id=<?=$ans['id']?>"><i class="icon-edit icon-2x"></i></a>
          <!--Delete--->
          <a onClick="return confirm('Are you sure to delete?')" href="?mode=del&id=<?=$id?>&search=<?=$search?>&page=<?=$currentPage?>&field=<?=$field?>&order=<?=$order?>"><i class="icon-trash icon-2x"></i></a>
        </td>
      </tr>
<?php
  }//end while
?>
    </tbody>

  </table>
</div>

<ul class="pagination pagination-lg">

  <li class="disabled"><a href="#">&laquo;</a></li>

<?php
  for ($i=1; $i <= $total_pages ; $i++) {
    if($i==$currentPage) {
?>
  <li class="active"><a href="#"><?=$i?> <span class="sr-only">(current)</span></a></li>

<?php
    } else { //end current page if()
?>
  <li><a href="?search=<?=$search?>&page=<?=$i?>&field=<?=$field?>&order=<?=$order?>"><?=$i?></a></li>
<?php
    } // end else
  }//end for() for paging
?>

  <li><a href="#">&raquo;</a></li>
</ul>


<!------ All Users End --------->
    </div> <!-- /container -->
<?php
  require_once('includes/footer.php');
?>
